//
//  H1_ImagesCell.m
//  Juxin
//
//  Created by WANGWEI on 16/3/14.
//  Copyright © 2016年 季诺. All rights reserved.
//

#import "H1_ImagesCell.h"
#import "TweetSendImageCCell.h"
#import "UICustomCollectionView.h"
#import "MJPhotoBrowser.h"
#import "UIView+Frame.h"

@interface H1_ImagesCell()
@property (nonatomic, strong) UILabel *twoLabel;
@property (strong, nonatomic) UICustomCollectionView *mediaView;
@property (strong, nonatomic) NSMutableDictionary *imageViewsDict;

@end

@implementation H1_ImagesCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor whiteColor];
        if (!self.mediaView) {
            if (!_titleLabel) {
                _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kTitleLeft, kTitleTop, kTitleWidth, kTitleHeight)];
                _titleLabel.font = [UIFont systemFontOfSize:14];
                _titleLabel.textColor = [UIColor grayColor];
                _titleLabel.numberOfLines = 0;
                //                _titleLabel.text = @"图片";
                [self.contentView addSubview:_titleLabel];
            }
            
            UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
            self.mediaView = [[UICustomCollectionView alloc] initWithFrame:CGRectMake(kPaddingLeftWidth, kMediaViewTop, kScreen_Width-2*kPaddingLeftWidth, 80) collectionViewLayout:layout];
            self.mediaView.scrollEnabled = NO;
            [self.mediaView setBackgroundView:nil];
            [self.mediaView setBackgroundColor:[UIColor clearColor]];
            [self.mediaView registerClass:[TweetSendImageCCell class] forCellWithReuseIdentifier:kCCellIdentifier_TweetSendImage];
            self.mediaView.dataSource = self;
            self.mediaView.delegate = self;
            [self.contentView addSubview:self.mediaView];
        }
        if (!_imageViewsDict) {
            _imageViewsDict = [[NSMutableDictionary alloc] init];
        }
    }
    return self;
}

-(void)setTitleColor:(UIColor *)color{
    _titleLabel.textColor = color;
}

- (void)setTitle:(NSString*)title
{
    _titleLabel.text = title;
    _titleLabel.frame = CGRectMake(kTitleLeft, kTitleTop, kTitleWidth, [H1_ImagesCell heightForString:title]);
    if([title isEqualToString:@""]){
        //此方法是见证取样附件下移动方法
        _mediaView.frame = CGRectMake(kPaddingLeftWidth, kTitleTop + kGapV + [H1_ImagesCell heightForString:_titleLabel.text] + 10, kScreen_Width-2*kPaddingLeftWidth,_mediaView.frame.size.height);
        
    }
}

- (void)setSubTitle:(NSString *)title
{
    _twoLabel.text = title;
}

- (void)setCurTweet:(Tweet *)curTweet{
    
    if (_curTweet != curTweet) {
        _curTweet = curTweet;
    }
    
    [self.mediaView setHeight:[H1_ImagesCell cellHeightWithObj:_curTweet title:_titleLabel.text]];
    
    [_mediaView reloadData];
}

+ (CGFloat)cellHeightWithObj:(id)obj title:(NSString *)title{
    CGFloat cellHeight = kTitleTop + kGapV + [H1_ImagesCell heightForString:title];
    if ([obj isKindOfClass:[Tweet class]]) {
        Tweet *curTweet = (Tweet *)obj;
        NSInteger row;
        row = ceilf((float)(curTweet.tweetImages.count +1)/2.0);
        cellHeight += (([TweetSendImageCCell ccellSize].height +10) *row)+100;
    }
    return cellHeight;
}

+ (CGFloat)heightForString:(NSString *)string
{
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14.] forKey:NSFontAttributeName];
    CGRect bounds = [string boundingRectWithSize:CGSizeMake(kTitleWidth, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return bounds.size.height;
}

#pragma mark Collection M
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSInteger num = _curTweet.selectedAssetURLs.count;
    //设置最多选择图片
    if(self.isNoADD==YES){
        return num < 9? num: num;
        
    }else{
        if(self.isEdit){
            return num < 9? num+ 1: num;
        }else{
            return num < 9? num: num;
        }
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    __weak typeof(self) weakSelf = self;
    
    TweetSendImageCCell *ccell = [collectionView dequeueReusableCellWithReuseIdentifier:kCCellIdentifier_TweetSendImage forIndexPath:indexPath];
    ccell.isEdit=self.isEdit;
    
    NSString *urlStr=@"";
    
    if (indexPath.row < _curTweet.selectedAssetURLs.count) {
        
        TweetImage *curImage = [_curTweet.tweetImages objectAtIndex:indexPath.row];
        urlStr = [_curTweet.selectedAssetURLs objectAtIndex:indexPath.row];
        
        //图片传递到自定义cell
        ccell.curTweetImg = curImage;
        
    }else{
        ccell.curTweetImg = nil;
    }
    ccell.deleteTweetImageBlock = ^(TweetImage *toDelete){
        if (weakSelf.deleteTweetImageBlock) {
            weakSelf.deleteTweetImageBlock(toDelete);
        }
        
        if(weakSelf.deleteTweetImageBlockUrl){
            weakSelf.deleteTweetImageBlockUrl(toDelete,urlStr);
        }
    };
    
    [_imageViewsDict setObject:ccell.imgView forKey:indexPath];
    
    return ccell;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return [TweetSendImageCCell ccellSize];
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsZero;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == _curTweet.tweetImages.count) {
        if (_addPicturesBlock) {
            _addPicturesBlock();
        }
    }else{
        NSMutableArray *photos = [NSMutableArray arrayWithCapacity:_curTweet.tweetImages.count];
        for (int i = 0; i < _curTweet.tweetImages.count; i++) {
            TweetImage *imageItem = [_curTweet.tweetImages objectAtIndex:i];
            MJPhoto *photo = [[MJPhoto alloc] init];
            photo.srcImageView = [_imageViewsDict objectForKey:indexPath]; // 来源于哪个UIImageView
            photo.image = imageItem.image; // 图片路径
            [photos addObject:photo];
        }
        // 2.显示相册
        MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];
        browser.currentPhotoIndex = indexPath.row; // 弹出相册时显示的第一张图片是？
        browser.photos = photos; // 设置所有的图片
        browser.showSaveBtn = NO;
        [browser show];
    }
}

- (void)didReceiveMemoryWarning{
    
    DeLog(@"附件内存警告");
    
    
}

@end
